package com.canbot.u05.activity.customvoice.adapter

import android.app.ActionBar
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.AnswerBean
import com.canbot.u05.glide.GlideApp
import com.canbot.u05.socket.IpMsgConst
import kotlinx.android.synthetic.main.custom_dialog_add_answer_item.view.*
import kotlinx.android.synthetic.main.custom_dialog_add_answer_item_menu.view.*

/**
 * CustomDialogAddAnswerAdapter
 *
 * @author  Liu.Heng
 * @date  2019/5/31
 */
class CustomDialogAddAnswerAdapter(val context: Context, var list: MutableList<AnswerBean>, var call: Callback) : RecyclerView.Adapter<CustomDialogAddAnswerAdapter.MyViewHolder>() {

    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    private val mCallback = call

    private var mList = list

    private lateinit var popupWindow: PopupWindow


    interface Callback {

        /**
         * 回调给主界面方法
         *
         * @param position 下标
         */
        fun clickEvent(position: Int, view: View)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.custom_dialog_add_answer_item, p0, false)
        return MyViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    fun removeItem(position: Int) {
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, mList.size - position)
    }

    override fun onBindViewHolder(myViewHolder: MyViewHolder, position: Int) {
        val delete1 = myViewHolder.delete1
        val relativeLayout = myViewHolder.relativeLayout
        val imageView1 = myViewHolder.imageView1
        val imageView2 = myViewHolder.imageView2
        val myTextView1 = myViewHolder.myTextView1
        val myTextView2 = myViewHolder.myTextView2
        val myTextView3 = myViewHolder.myTextView3
        val myTextView4 = myViewHolder.myTextView4

        val answerBean = list[position]
        if (answerBean.imgpath.isNotEmpty()) {
            myTextView3.text = "一张图片"
            GlideApp.with(context).clear(imageView1)
            val requestOptions = RequestOptions.circleCropTransform()
            GlideApp.with(context)
                    .load(IpMsgConst.HTTP_URL_CHEST + answerBean.imgpath.replace("/uupic/", "/uupic_thumb/"))
                    .thumbnail(0.1f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                    .skipMemoryCache(false)
                    .placeholder(R.drawable.custom_dialog2_answer_pic_moren)//占位图
                    .apply(requestOptions).into(imageView1)
        } else {
            GlideApp.with(context).clear(imageView1)
            GlideApp.with(context).load(R.drawable.custom_dialog2_answer_item_add).into(imageView1)
            myTextView3.text = "添加图片"
        }
        if (answerBean.actionid.isEmpty()) {
            myTextView4.text = "默认动作"
        } else {
            myTextView4.text = "一个动作"
        }
        if (answerBean.type == 1) {
            myTextView1.text = "说一段话"
            myTextView2.text = answerBean.ttstext
            myTextView2.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        } else {
            myTextView1.text = "播放音频"
            myTextView2.text = ""
            myTextView2.setCompoundDrawablesWithIntrinsicBounds(context.getDrawable(R.drawable.custom_dialog2_answer_voc), null, null, null)
        }
        delete1.setOnClickListener {
            mCallback.clickEvent(position, it)
        }
        relativeLayout.setOnClickListener {
            mCallback.clickEvent(position, it)
        }
        imageView1.setOnClickListener {
            mCallback.clickEvent(position, it)
        }
        imageView2.setOnClickListener {
            showPopupWindow(it, position)
        }
    }

    /**弹框的显示*/
    private fun showPopupWindow(view: View, position: Int) {

        /** 一个自定义的布局，作为显示的内容*/
        val contentView = View.inflate(context, R.layout.custom_dialog_add_answer_item_menu, null)

        val relativeLayout = contentView.my_relativeLayout
        val relativeLayout1 = contentView.my_relativeLayout1

        val imageView = contentView.imageView_check

        val answerBean = list[position]
        if (answerBean.actionid.isEmpty()) {
            imageView.visibility = View.VISIBLE
        } else {
            imageView.visibility = View.GONE
        }

        relativeLayout.setOnClickListener {
            popupWindow.dismiss()
            mCallback.clickEvent(position, it)
        }

        relativeLayout1.setOnClickListener {
            popupWindow.dismiss()
            mCallback.clickEvent(position, it)
        }

        popupWindow = PopupWindow(contentView,
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true)

        popupWindow.setTouchInterceptor { _, _ ->
            Log.i("mengdd", "onTouch : ")
            false
            /**这里如果返回true的话，touch事件将被拦截*/
            /**拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss*/
        }

        /**如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框*/
        popupWindow.setBackgroundDrawable(context.getDrawable(R.drawable.custom_dialog2_add_answer_item_menu_box))

        /**设置好参数之后再show*/
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        popupWindow.showAsDropDown(view, context.resources.getDimension(R.dimen.px220).toInt(), -context.resources.getDimension(R.dimen.px80).toInt())

    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var delete1: Button = view.delete1
        var relativeLayout: RelativeLayout = view.answer_relativeLayout_item
        var imageView1: ImageView = view.my_imageView1
        var imageView2: ImageView = view.my_imageView2
        var myTextView1: TextView = view.my_text1
        var myTextView2: TextView = view.my_text2
        var myTextView3: TextView = view.my_text3
        var myTextView4: TextView = view.my_text4
    }
}